package com.hospital.warehouse.drugstock.service.impl;

import java.text.ParseException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hospital.util.DTO;
import com.hospital.util.JMyUtil;
import com.hospital.warehouse.drugstock.dao.DrugstockDao;
import com.hospital.warehouse.drugstock.entity.Drugstock;
import com.hospital.warehouse.drugstock.service.DrugstockService;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Mr. YR
 * @since 2020-04-21
 */
@Service
public class DrugstockServiceImpl extends ServiceImpl<DrugstockDao, Drugstock> implements DrugstockService {

	@Autowired
	DrugstockDao storehouseDao;

	@Override
	public DTO paging(Drugstock po, Integer page, Integer limit) throws ParseException {
//		System.out.println(po);

		if (po.getInstock() != null) {
			if (po.getInstock().getIs_enddate_interval() != null
					&& JMyUtil.notEmpty(po.getInstock().getIs_enddate_interval())) {
				String[] dates = po.getInstock().getIs_enddate_interval().split(" ~ ");
				po.getInstock().setIs_enddate_min(JMyUtil.formatStringToDate_yMd(dates[0]));
				po.getInstock().setIs_enddate_max(JMyUtil.formatStringToDate_yMd(dates[1]));
			}
		}

		Page<Drugstock> pageinfo = new Page<>(page, limit);

		IPage<Drugstock> iPage = storehouseDao.paging(po, pageinfo, null);
		List<Drugstock> list = iPage.getRecords();

		DTO dto = new DTO();
		dto.setCount(iPage.getTotal());
		dto.setData(list);

		if (list.size() <= 0) {
			dto.setMsg("没有记录");
		}

		return dto;
	}

	/**
	 * 通过药品id查询现有库存(已验收的入库单)，获得可用的批次
	 */
	@Override
	public DTO queryByDrugId(Integer id) {
		List<Drugstock> list = storehouseDao.queryByDrugId(id);
		DTO dto = new DTO();
		dto.setData(list);
		if (list == null || list.size() <= 0) {
			dto.setMsg("该药品没有存货");
		} else {
			dto.setMsg("查询到" + list.size() + "种批次，请选择。");
		}
		return dto;
	}
}
